package com.oracle.oa.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.MyNoteBiz;
import com.oracle.oa.entity.Mynote;
import com.oracle.oa.entity.Userinfo;

public class MyNoteAction extends ActionSupport implements SessionAware,
		ServletResponseAware {

	private static final long serialVersionUID = 1L;
	private Mynote mynote;
	private Mynote myno;
	public Long id;
	public MyNoteBiz mbiz;
	public Userinfo userinfo;
	public String url;

	private javax.servlet.http.HttpServletResponse response;
	public Map<String, Object> session;

	/**
	 * 增加
	 */
	public String add() {
		Userinfo userinfo = (Userinfo) session.get("userinfo");

		mynote.setUserinfo(userinfo);
		// mynote.setNoteid(u);

		mbiz.save(mynote);

		@SuppressWarnings("unchecked")
		List<Mynote> my = mbiz.getByProperty(Mynote.class, "userinfo.userid",
				userinfo.getUserid());

		session.put("my", my); // 查看个人标签

		url = "/admin_cg/date_yz/noteList.jsp";

		return SUCCESS;
	}

	/**
	 * 删除
	 */
	public String delete() {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");// 防止弹出的信息出现乱码
		// 以上2句代码一定要写在获取out对象之前，否则提示信息为乱码

		Userinfo userinfo = (Userinfo) session.get("userinfo");
		
		// 根据id获取mynote对象
		mynote = (Mynote) mbiz.getById(Mynote.class, myno.getNoteid());
		mbiz.delete(mynote); // 执行删除

		
//		便签信息修改，只能查改个人便签（张冰月/6月12号改）
		List<Mynote> my=mbiz.getByProperty(Mynote.class, "userinfo.userid",userinfo.getUserid());
        
		session.put("my", my);
		
		
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.print("<script>alert('删除成功！')</script>");
			out.print("<script>window.location.href='../admin_cg/date_yz/noteList.jsp"
					+ "'</script>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}

		return null;
	}

	/**
	 * 更改
	 */
	public String update() {

		mynote = (Mynote) mbiz.getById(Mynote.class, myno.getNoteid());

		userinfo = mynote.getUserinfo();
		myno.setUserinfo(userinfo);

		mbiz.update(myno); // update方法
		
		Userinfo userinfo = (Userinfo) session.get("userinfo");

//		便签信息修改，只能查改个人便签（张冰月/6月12号改）
		List<Mynote> my=mbiz.getByProperty(Mynote.class, "userinfo.userid",userinfo.getUserid());
        
		session.put("my", my);
		
		url = "/admin_cg/date_yz/noteList.jsp";

		return SUCCESS;
	}

	/**
	 * 查找所有
	 */
	public String findAll() {
		List<Mynote> mynoteList = mbiz.getAll(Mynote.class);
		session.put("mynoteList", mynoteList);
		url = "/admin_cg/date_yz/noteList.jsp";
		return SUCCESS;
	}

	/**
	 * 根据id查找
	 */
	public String findOne() {

		Mynote mn = (Mynote) mbiz.getById(Mynote.class, id);

		session.put("mn", mn);

		url = "/admin_cg/date_yz/noteupdate.jsp";

		return SUCCESS;
	}

	public MyNoteBiz getMbiz() {
		return mbiz;
	}

	public void setMbiz(MyNoteBiz mbiz) {
		this.mbiz = mbiz;
	}

	public Mynote getMynote() {
		return mynote;
	}

	public void setMynote(Mynote mynote) {
		this.mynote = mynote;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setSession(Map<String, Object> arg0) {

		this.session = arg0;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public Mynote getMyno() {
		return myno;
	}

	public void setMyno(Mynote myno) {
		this.myno = myno;
	}

	public Userinfo getUserinfo() {
		return userinfo;
	}

	public void setUserinfo(Userinfo userinfo) {
		this.userinfo = userinfo;
	}

	public void setServletResponse(HttpServletResponse arg0) {

		this.response = arg0;

	}

}
